package com.quanshu.exchange.support.db.core;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.quanshu.exchange.support.entity.GroupEntity;
import com.quanshu.exchange.support.entity.UserEntity;
import com.quanshu.exchange.support.utils.AppUtils;

import java.sql.SQLException;

public class OrmDBHelper extends OrmLiteSqliteOpenHelper {

    public OrmDBHelper(Context context, String path, int databaseVersion) {
        super(context, AppUtils.getAppName(context) + path, null, databaseVersion);
    }

    @Override
    public void onCreate(SQLiteDatabase database, ConnectionSource source) {
        try {
            //创建表
            TableUtils.createTable(source, UserEntity.class);
            TableUtils.createTable(source, GroupEntity.class);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, ConnectionSource source, int oldVersion, int newVersion) {

        //新增、变更、删除表操作
        try {
            TableUtils.dropTable(source, UserEntity.class, true);
            TableUtils.dropTable(source, GroupEntity.class, true);
            onCreate(database, source);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //释放资源
    @Override
    public void close() {
        super.close();
    }

}
